#!/bin/sh

# Copyright 2021 Balena Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Keep supervisor database when updating from releases between v2.80.0 > r < v2.80.10 upwards
#

set -e

# shellcheck disable=SC1091
. /usr/libexec/os-helpers-logging

#
# MAIN
#

DATA_MOUNT="/mnt/data"
SUPERVISOR_DB_DIR_A="${DATA_MOUNT}/resin-data/balena-supervisor"
SUPERVISOR_DB_DIR_B="${DATA_MOUNT}/resin-data/resin-supervisor"
SUPERVISOR_DB_NAME="database.sqlite"

SUPERVISOR_DB_PATH_A="${SUPERVISOR_DB_DIR_A}/${SUPERVISOR_DB_NAME}"
SUPERVISOR_DB_PATH_B="${SUPERVISOR_DB_DIR_B}/${SUPERVISOR_DB_NAME}"

if [ -f "${SUPERVISOR_DB_PATH_B}" ]; then
	if [ -f "${SUPERVISOR_DB_PATH_A}" ]; then
		last_modified_a=$(stat -c %Y "${SUPERVISOR_DB_PATH_A}")
		last_modified_b=$(stat -c %Y "${SUPERVISOR_DB_PATH_B}")
		if [ "${last_modified_a}" -gt "${last_modified_b}" ]; then
			info "fwd_commit_supervisor-db: Removed unused ${SUPERVISOR_DB_DIR_B}"
			rm -rf "${SUPERVISOR_DB_DIR_B}"
			exit
		else
			warn "fwd_commit_supervisor-db: ${SUPERVISOR_DB_PATH_B} is newer than ${SUPERVISOR_DB_PATH_A} - bailing out"
		fi
	fi
fi
